home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr23 / chansw53.zip / CHAIN2.CPP next >
C/C++ Source or Header  |  1993-05-24  |  6KB  |  185 lines

  1.  
  2. // FILE: CHAIN2.CPP
  3. // MODULE: OBJECT
  4. // PROGRAM: CHAINSAW 
  5.  
  6. // Public Domain, 5/24/93, Ted Davis
  7.  
  8.  
  9. #include "chainsaw.hpp"
  10.  
  11.  
  12. dirkiller::dirkiller(char StartingPath[], int KillFilesFlag, int KillDirFlag)
  13. // The constructor for dirkiller.  It is also the method of the object.
  14. // It searches the given directory for files, which it deletes, and for 
  15. // directories, which it recurses by creating and destroying a new instance
  16. // of itself which is passed the full path of the subdirectory.  It starts 
  17. // at the top of the given directory and cleans out the first item before 
  18. // repeating for the new first item.
  19. {
  20. int PassCount = 0;
  21. int flag = no;
  22. char * strptr;
  23. char TempString[80];
  24.     strcpy(WorkingPath, StartingPath);    
  25.     strcat(WorkingPath, "*.*");
  26.     while(!flag)
  27.     {
  28.         // If this is the first pass it is necessary to use findfirst(), 
  29.         // otherwise findnext().
  30.         if(PassCount++)
  31.         {
  32.             flag = findnext(&DataBlock);
  33.  
  34.             // If the directory is empty and the flags say remove it (which
  35.             // occurs only in the first instance of the object, the others 
  36.             // are removed by their parent object), set "flag" so that the
  37.             // following code will treat it as a special case.
  38.             if((flag == -1) && ((errno == EMFILE) || (errno == ENOFILE))\
  39.              && KillDirFlag && !IsRoot)
  40.             {flag = yes;}
  41.         }
  42.         else
  43.         {
  44.             flag = findfirst(WorkingPath, &DataBlock, ANY_FILE);
  45.  
  46.             // See comment for the identical line above.
  47.             if((flag == -1) && ((errno == EMFILE) || (errno == ENOFILE))\
  48.              && KillDirFlag && !IsRoot)
  49.             {flag = yes;}
  50.         }
  51.         // The return (flag) is 0 for success or +1 for
  52.         // remove this dir, or -1 for failure.
  53.  
  54.         if(flag > -1)    // If something was found in the directory (or forced).
  55.         {
  56.             strcpy(FileOrDirName, StartingPath);
  57.             // The following !flag's enable code for other than remove the
  58.             // given directory by testing for 0 (flag > -1 tested for 0 or +1).
  59.             if(!flag) {strcat(FileOrDirName, DataBlock.ff_name);}
  60.  
  61.             // Is it a directory, other than "." or ".." (which MUST not 
  62.             // be processed)?
  63.             if(((DataBlock.ff_attrib & FA_DIREC) && \
  64.             (DataBlock.ff_name[0] != '.') && \
  65.             (strncmp(DataBlock.ff_name, "..", 2))) || (flag == yes))
  66.             {
  67.                 // Add a trailing backslash.
  68.                 if(!flag) {strcat(FileOrDirName, "\\");}
  69.  
  70.                 // If it is a valid directory, spawn a new instance of
  71.                 // the dirkiller object to process it.
  72.                 if((NextLevel = new dirkiller(FileOrDirName, yes, no)) == no)
  73.                 {
  74.                     // This code executes if the next instance of dirkiller
  75.                     // cannot be created for any reason.
  76.                     Output(MemoryError);
  77.                     setcbrk(cbreak); exit(1);
  78.                 }
  79.                 else
  80.                 {
  81.                     // This frees the memory allocated by the "new" in the 
  82.                     // "if" condition.  Creation of the object causes the
  83.                     // constructor to execute, which does everything the
  84.                     // object can do; no special call is required.
  85.                     delete NextLevel;
  86.  
  87.                     // Remove the now empty subdirectory.
  88.                     // If the default directory is the one to be deleted, it is
  89.                     // necessary to change away from it.
  90.  
  91.                     // Make a pointer to the directory part of the string.
  92.                     strptr = FileOrDirName + 3;
  93.  
  94.                     // Save the default directory on the target drive.
  95.                     TargetDrive = StartingPath[0] - 'A' + 1;
  96.                     getcurdir(TargetDrive, CurrentDir);
  97.  
  98.                     // add a trailing '\' to match FileOrDirName.
  99.                     strcat(CurrentDir, "\\");
  100.  
  101.                     // Compare the default directory on the target drive with
  102.                     // the one in work.  If they are the same, move toward 
  103.                     // the root.
  104.                     if(!strcmp(strptr, CurrentDir))
  105.                     {
  106.                         strncpy(TempString, StartingPath, 2);
  107.                         TempString[2] = '\0';
  108.                         strcat(TempString, "..");
  109.                         // That makes a string of the form "C:.." which
  110.                         // lets chdir() back out by one level.
  111.                         chdir(TempString);
  112.                     }
  113.  
  114.                     // Now it is ok to attempt to remove the directory
  115.                     // in work.
  116.                     // rmdir() requires the absense of a trailing "\".
  117.                     FileOrDirName[(strlen(FileOrDirName) - 1)] = '\0';                    
  118.                     if(!rmdir(FileOrDirName))
  119.                     {
  120.                         // If the removal succeeded.
  121.                         PrintDirOkMessage(FileOrDirName);
  122.                     }
  123.                     else
  124.                     {
  125.                         // If it failed.
  126.                         PrintDirFailedMessage(FileOrDirName);
  127.                         ErrorLevel = 2;
  128.                     }
  129.                 }
  130.             }
  131.             else
  132.             {
  133.                 // Delete the files, one per pass of the "while" loop.
  134.                 if(KillFilesFlag && (!(DataBlock.ff_attrib & FA_DIREC)))
  135.                 {
  136.                     if(DataBlock.ff_attrib & FA_RDONLY) 
  137.                     {
  138.                         if(ProtectedOK) {make_writable;}
  139.                     }
  140.                     if((DataBlock.ff_attrib & FA_HIDDEN) || \
  141.                     (DataBlock.ff_attrib & FA_SYSTEM) || \
  142.                     (DataBlock.ff_attrib & FA_RDONLY))
  143.                     {
  144.                         if(!ProtectedOK) 
  145.                         {
  146.                             // Do not erase read-only, system, or hidden
  147.                             // files without special permission.  Instead,
  148.                             // display an explanatory error message.
  149.                             PrintFileProtected(FileOrDirName);
  150.                         }
  151.                         else
  152.                         {
  153.                             // Make file writable so it can be erased.
  154.                             make_writable;
  155.                             // If permitted, try to delete the protected file.
  156.                             if(!unlink(FileOrDirName))    
  157.                             {
  158.                                 PrintFileOkMessage(FileOrDirName);
  159.                             }
  160.                             else
  161.                             {
  162.                                 PrintFileFailedMessage(FileOrDirName);
  163.                                 ErrorLevel = 2;
  164.                             }
  165.                         }
  166.                     }
  167.                     else
  168.                     {
  169.                         if(!unlink(FileOrDirName))    
  170.                         {
  171.                             PrintFileOkMessage(FileOrDirName);
  172.                         }
  173.                         else
  174.                         {
  175.                             PrintFileFailedMessage(FileOrDirName);
  176.                             ErrorLevel = 2;
  177.                         }
  178.                     }
  179.                 }
  180.             }
  181.         }
  182.     }
  183. }
  184.  
  185.